from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from time import sleep
import pyautogui
import re


class StockOutPage(BasePage):
    url = '/manage/stock/stock-out'
    waitpay_first_uniquecode_locater = (By.XPATH, '//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[1]/span')
    unicode_search_locater = (By.XPATH, '//*[@id="uniqueCode"]')
    search_locater = (By.XPATH, '//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[1]/form/div/di''v[8]/div/div/div[2]/div/div/div/div[1]/div/button[2]')
    wait_outstock_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div/div[1]/div/div/div/label[2]/div')
    waitoutstock_first_unicode_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[2]/span')
    waitoutstock_second_unicode_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[3]/td[2]/span')
    first_finish_stock_out_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[12]/div/button/span')
    confirm_stock_out_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[2]/button[2]/span')
    out_stocked_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div/div[1]/div/div/div/label[3]/div')
    first_stockouted_unicode_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[2]/span')
    batch_stock_out_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div/div[2]/div[1]/button[2]/span')
    waitoutstock_first_ckno_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[2]/td[1]/span')
    waitoutstock_second_ckno_locater = (By.XPATH,'//*[@id="root"]/div/div/div[2]/div[2]/main/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/table/tbody/tr[3]/td[1]/span')
    batch_stock_out_search_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[2]/form/div[1]/div/div[2]/div/div/span/span/span[1]/input')
    batch_stock_out_confirm_locater = (By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div[3]/div/div/button[2]/span')

    def get_first_waitpay_uniquecode(self):
        """获取第一个待支付库存唯一码"""
        return self.get_text(self.waitpay_first_uniquecode_locater)

    def search_unicode(self, unicode):
        """通过唯一码搜索库存"""
        self.send_keys(self.unicode_search_locater,unicode)
        self.click(self.search_locater)

    def click_wait_outstock(self):
        """点击待出库tab"""
        self.click(self.wait_outstock_locater)

    def get_first_waitoutstock_unicode(self):
        """获取第一个待出库库存唯一码"""
        return self.get_text(self.waitoutstock_first_unicode_locater)

    def get_second_waitoutstock_unicode(self):
        """获取第二个待出库库存唯一码"""
        return self.get_text(self.waitoutstock_second_unicode_locater)

    def get_first_waitoutstock_ckno(self):
        """获取第一个待出库库存出库单号"""
        return self.get_text(self.waitoutstock_first_ckno_locater)

    def get_second_waitoutstock_ckno(self):
        """获取第二个待出库库存出库单号"""
        return self.get_text(self.waitoutstock_second_ckno_locater)

    def switch_pay_records(self):
        """切换到支付记录模块"""
        self.click_position(126,364)

    def finish_first_stock_out(self):
        """完成第一个待出库库存出库"""
        self.click(self.first_finish_stock_out_locater)
        self.click(self.confirm_stock_out_locater)

    def click_stocked_outed(self):
        """点击出库管理的已出库tab"""
        self.click(self.out_stocked_locater)

    def get_first_stockouted_uniquecode(self):
        """获取第一个已出库库存的唯一码"""
        return self.get_text(self.first_stockouted_unicode_locater)

    def get_wait_outstock_num(self):
        """获取待出库数量"""
        text = self.get_text(self.wait_outstock_locater)
        numbers = re.findall(r'\d+', text)
        return int(numbers[0])

    def click_batch_stock_out(self):
        """点击批量出库"""
        self.click(self.batch_stock_out_locater)
        pyautogui.press('capslock')

    def batch_ckno_search(self,text):
        """批量出库搜索出库单号"""
        self.send_keys(self.batch_stock_out_search_locater,text=text)
        # self.click_position(646, 272)
        # self.sendkeys_position(text)
        sleep(2)

    def confirm_batch_stock_out(self):
        """确认批量出库"""
        self.click(self.batch_stock_out_confirm_locater)